package genericdemo;

import java.lang.reflect.Array;

class MyArray<T>{
    public Object[] obj = new Object[3];
    public T getPos(int pos){
        return (T)obj[pos];
    }
    public void setObj(int pos,T val){
        obj[pos] = val;
    }
}


class MyArray1<E>{
    public E[] obj = (E[])new Object[3];
    public E[] getArray(){
        return obj;
    }
    public void setObj(int pos,E val){
        obj[pos] = val;
    }
}


class MyArray2<E>{
    public E[] obj ;
    public MyArray2(Class<E> clazz, int capacity) {
        obj = (E[])Array.newInstance(clazz, capacity);
    }
    public E[] getArray(){
        return obj;
    }

    public void setObj(int pos,E val){
        obj[pos] = val;
    }
}
public class Test {
    public static void main3(String[] args) {
        MyArray2<Integer> myArray2 = new MyArray2<>(Integer.class,10);
        //这里就相当于我已经指定了数组的类型是Integer
        myArray2.setObj(0,10);
        myArray2.setObj(1,100);

        Integer[] integers = (Integer[])myArray2.getArray();

    }
    public static void main(String[] args) {
        MyArray1<Integer> myArray1 = new MyArray1<>();
        myArray1.setObj(0,10);
        myArray1.setObj(1,71);
        Integer[] integers = (Integer[])myArray1.getArray();
    }
    public static void main2(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        //就可以存放指定的数据类型
        myArray.setObj(0,10);
        myArray.setObj(1,71);
        myArray.setObj(2,101);
        Integer a = myArray.getPos(1);
        System.out.println(a);
    }



    public static void main1(String[] args) {
        //实例化对象的时候 指定当前泛型类的指定参数类型是Integer
        MyArray<Integer> myArray = new MyArray<Integer>();
        //就可以存放指定的数据类型
        myArray.setObj(0,10);
//        myArray.setObj(1,"hello");
//        myArray.setObj(2,10.0);



        double d = myArray.getPos(2);
        Object d1 = myArray.getPos(2);

        System.out.println(d);
    }
}
